import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_lib/utils/ToastUtils.dart';
import 'package:flutter_lib/utils/ttf_parser/ttf_parser.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/AppBarTitleWidget.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';
import 'package:get/get.dart';


class IconManagerPage extends StatelessWidget {
  var icons = [].obs;


  @override
  Widget build(BuildContext context) {
    return Scaffold( appBar: AppBar(
        elevation: 0,
        title: AppBarTitleWidget(
          "桌面GO图标查看器",
        ),
        centerTitle: true,
        leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
            onPressed: () {
              Get.back();
            })),body: Column(children: [
      BlueMaterialButton("选择ttf文件", () async {
        var retPaths =
        await FilePicker.platform.pickFiles(allowMultiple: false);
        Uint8List? bytes;
        if (retPaths != null && retPaths.count > 0) {
          if (GetPlatform.isWeb){
            bytes = retPaths.files[0].bytes;
          }else{
            var fontPath = retPaths.files[0].path;
            LogUtils.e("FontPath:${fontPath}");
            File fontFile = File(fontPath!);
            bytes = fontFile.readAsBytesSync();
          }
          if (bytes == null){
            showSnackBar("内容 为空");
            return;
          }
          var mTtfFont = TtfParser().parse(bytes);
          // LogUtils.e("name:${mTtfFont.name.fontName}");
          // LogUtils.e("name:${mTtfFont.cmap.charToGlyphIndexMap}");
          // LogUtils.e("name:${mTtfFont.cmap.glyphToCharIndexMap}");
          var fontLoader = FontLoader("testFamily1");
          await fontLoader.loadFont(bytes, "testFamily1");
          var tempList = [];
          mTtfFont.cmap.charToGlyphIndexMap.forEach((key, value) {
            tempList.add(key);
          });

          // for(var i = 0;i<0xffff;i++){
          //   tempList.add(i);
          // }
          icons.value = tempList;
        }
      }),
      Expanded(
          child: Obx(() => GridView.builder(
              itemCount: icons.length,
              gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 120.0, childAspectRatio: 2.0 //宽高比为2
              ),
              itemBuilder: (context, index) {
                return Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        color: Colors.black,
                          height:30,
                          width: 30,
                          child: Center(child: Icon(IconData(icons.value[index], fontFamily: 'testFamily1'),color: Colors.white,))),
                      SelectableText("${(icons.value[index] as int).toRadixString(16)}"),
                    ],
                  ),
                );
              })))
    ],),);
  }
}
